/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.knezevic.realestate.pages;

import com.knezevic.realestate.Member;
import com.knezevic.realestate.Privilege;
import com.knezevic.realestate.beans.MemberBean;
import com.knezevic.realestate.service.Members;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 *
 * @author Nemanja
 */
public class Register {
    
    @Property
    private MemberBean memberBean;
    @Persist("flash")
    @Property
    private String message;
    @Inject
    private Members members;
    

    void onActivate(){
        memberBean = new MemberBean();
    }

    Object onSubmitFromMemberBean(){
        for (Member member : members.retrieve()) {
            if(member.getUsername().equalsIgnoreCase(memberBean.getUsername())){
                message = "Username already in use!";
                return null;
            }if(member.getEmail().equalsIgnoreCase(memberBean.getEmail())){
                message = "Email already in use!";
                return null;
            }
        }memberBean.setPrivilege(Privilege.REGULAR);
         members.save(memberBean);
         memberBean = null;
         message = "Successfull registration!";
         return null;
    }

}
